package com.logistics.user;

import java.awt.Window;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.spi.DirStateFactory.Result;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.logistics.database.Distributiondatabase;
import com.logistics.database.Userdatabase;

public class Userqueryservlet extends HttpServlet {


	public Userqueryservlet() {
		super();
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		//System.out.println(request.getParameter("storeroom_name"));
		//System.out.println(request.getParameter("product_name"));
		String type=request.getParameter("type");
		System.out.println("type="+type);
		if(type.equals("query"))
		{
			String userquery_id=new String(request.getParameter("userquery_id").getBytes("iso-8859-1"),"utf-8");
			Userdatabase udb1 = new Userdatabase();
			ResultSet rs1=udb1.userQuery1(userquery_id);
			
			Userdatabase udb2 = new Userdatabase();
			ResultSet rs2=udb2.userQuery2(userquery_id);
			
			Userdatabase udb3 = new Userdatabase();
			ResultSet rs3=udb3.userQuery3(userquery_id);
			
			try {
				if(rs1.next())
				{
					request.setAttribute("u_name", rs1.getString("user_name"));
					request.setAttribute("user_nick", rs1.getString("user_nick"));
					request.setAttribute("company", rs1.getString("company"));
					request.setAttribute("cellphone", rs1.getString("cellphone"));
					request.setAttribute("tel", rs1.getString("tel"));
					request.setAttribute("id_card", rs1.getString("id_card"));
				}
				else if(rs2.next())
				{
					request.setAttribute("u_name", rs2.getString("user_name"));
					request.setAttribute("user_nick", rs2.getString("user_nick"));
					request.setAttribute("company", rs2.getString("company"));
					request.setAttribute("cellphone", rs2.getString("cellphone"));
					request.setAttribute("tel", rs2.getString("tel"));
					request.setAttribute("id_card", rs2.getString("id_card"));
				}
				else if(rs3.next())
				{
					request.setAttribute("u_name", rs3.getString("user_name"));
					request.setAttribute("user_nick", rs3.getString("user_nick"));
					request.setAttribute("company", rs3.getString("company"));
					request.setAttribute("cellphone", rs3.getString("cellphone"));
					request.setAttribute("tel", rs3.getString("tel"));
					request.setAttribute("id_card", rs3.getString("id_card"));
				}
			} 
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.getRequestDispatcher("../user/Userquery.jsp").forward(request, response);  
		}
		else if(type.equals("delete"))
		{
			String userid=new String(request.getParameter("userid").getBytes("iso-8859-1"),"utf-8");
			Userdatabase udb4 = new Userdatabase();
			ResultSet rs4=udb4.orderQuery3(userid);
			try {
				if(rs4.next())
				{
					System.out.println("delete failed!");
					request.setAttribute("deleteuser", "failed");
				}
				else
				{
					udb4.userDelete(userid);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("delete");
			request.getRequestDispatcher("../user/Userquery.jsp").forward(request, response);
		}
		else if(type.equals("update"))
		{
			String userid=new String(request.getParameter("userid").getBytes("iso-8859-1"),"utf-8");
			Userdatabase udb5 = new Userdatabase();
			ResultSet rs5=udb5.userQuery1(userid);
			Userdatabase udb6 = new Userdatabase();
			ResultSet rs6=udb6.orderQueryByUsername(userid);
			ArrayList<Order> orders=new ArrayList<Order>();
			try {
				if(rs5.next())
				{
					request.setAttribute("u_name", rs5.getString("user_name"));
					request.setAttribute("user_nick", rs5.getString("user_nick"));
					request.setAttribute("company", rs5.getString("company"));
					request.setAttribute("cellphone", rs5.getString("cellphone"));
					request.setAttribute("tel", rs5.getString("tel"));
					request.setAttribute("id_card", rs5.getString("id_card"));
				}
				while(rs6.next())
				{
					Order ord=new Order();
					ord.setOrder_id(Integer.toString(rs6.getInt("order_id")));
					ord.setOrder_state(rs6.getString("order_state"));					
					ord.setProduct_number(Integer.toString(rs6.getInt("product_number")));
					ord.setTotal_price(Double.toString(rs6.getDouble("total_price")));
					ord.setProduct_name(rs6.getString("product_name"));
					ord.setProduct_price(Double.toString(rs6.getDouble("price")));					
					orders.add(ord);	
					System.out.println(ord.toString());
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("orders.length="+orders.size());
			request.setAttribute("orders", orders);
			System.out.println("update");
			request.getRequestDispatcher("../user/Userinfo.jsp").forward(request, response);
		}
		else if(type.equals("insert"))
		{
			Userdatabase udb7 = new Userdatabase();
			String u_name=new String(request.getParameter("u_name").getBytes("iso-8859-1"),"utf-8");
			String user_nick=new String(request.getParameter("user_nick").getBytes("iso-8859-1"),"utf-8");
			String id_card=new String(request.getParameter("id_card").getBytes("iso-8859-1"),"utf-8");
			String company=new String(request.getParameter("company").getBytes("iso-8859-1"),"utf-8");
			String cellphone=new String(request.getParameter("cellphone").getBytes("iso-8859-1"),"utf-8");
			String tel=new String(request.getParameter("tel").getBytes("iso-8859-1"),"utf-8");
			udb7.userUpdate(u_name,user_nick,id_card,company,cellphone,tel);
			System.out.println("insert");
			request.getRequestDispatcher("../user/Userquery.jsp").forward(request, response);
		}
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
		
	}


}
